import re

from django import forms
from django.core.exceptions import ValidationError
from django.core.validators import validate_email
from django.core.exceptions import ValidationError

from users.models import UserProfile


def mobile_validate(value):
    mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
    try:
        if mobile_re.match(value) or validate_email(value):
            return True
        else:
            return False
    except ValidationError:
        raise ValidationError('手机号码或邮箱格式错误')


class LoginForm(forms.Form):
    class Meta:
        model = UserProfile
        fields = ['username', 'password']


class RegisterForm(forms.Form):
    class Meta:
        model = UserProfile
        fields = ['email', 'username', 'password', 'password']


class ForgetForm(forms.Form):
    class Meta:
        model = UserProfile
        fields = ['email']


class EditForm(forms.Form):
    class Meta:
        model = UserProfile
        fields = ['username', 'first_name', 'second_name', 'password', 'gender', 'email', 'mobile']
    # password = forms.CharField(required=True, min_length=4)
    # password1 = forms.CharField(required=True, min_length=4)
    # password2 = forms.CharField(required=True, min_length=4)


class UploadImageForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ['image']


class UserInfoForm(forms.ModelForm):
    class Meta:
        model = UserProfile
        fields = ['username', 'gender', 'birthday', 'address', 'mobile']